package com.hm.service.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.hm.service.service.wechat.IWeChatPublic;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;


@RestController
@RequestMapping(value = "/MAIN", produces = {"application/json;charset=UTF-8"})
public class WCPS_000_000_003 {
    private static final Logger logger = LoggerFactory.getLogger(WCPS_000_000_003.class);

    private final IWeChatPublic iWeChatPublic;

    public WCPS_000_000_003(IWeChatPublic iWeChatPublic) {
        this.iWeChatPublic = iWeChatPublic;
    }


    //微信js-sdk 注入权限验证配置 （所有需要使用JS-SDK的页面必须先注入配置信息，否则将无法调用!）
    @PostMapping(value = "/000_000_003")
    public String doPost(HttpServletRequest request) {
        try {
            String body = request.getReader().readLine();
            JSONObject reqObject = JSONObject.parseObject(body);
            String hrefUrl = reqObject.getString("hrefUrl");
            logger.error("hm-->" + hrefUrl);

            String accessToken = iWeChatPublic.getAccessToken();
            String jsapiTicket = iWeChatPublic.getJsapiTicket(accessToken);
            HashMap<String, String> map = iWeChatPublic.generateSignature(hrefUrl, jsapiTicket);
            String jsonString = JSON.toJSONString(map);
            logger.error("jsonString-->" + jsonString);
            return jsonString;

        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return "";
    }


}
